import axios from "axios";

export function api({ url, data, method, params }) {

  let newurl = ""
  // let newurl = process.env.NODE_ENV === 'dev' ? url :  address + url

  if (process.env.NODE_ENV==="dev") {  // 判断本地
    if(url.indexOf("flask")==1 || url.indexOf("v1") == 1){
      newurl = "http://localhost:5000"
    }else{
      newurl = "http://192.168.1.51:8000/api"
    }
  }else{
    if (url.indexOf("v1")== 1) {      // 远程环境
      let flask_api =  "http://62.234.22.56:8081"   // flask接口地址
      newurl = flask_api
    }else{
      let django_api  = 'http://62.234.59.209:8888/api'
      newurl = django_api
    }
  }
  let fullurl = newurl + url

  return new Promise((resolve, reject) => {
    axios({
      method,
      url:fullurl,
      data,
      params,
      headers: {
        Authorization: JSON.parse(localStorage.getItem("token"))
        // token:localStorage.getItem("token")
      }
    })
      .then(da => {
        resolve(da);
      })
      .catch(error => {
        reject(error);
      });
  });
}

